package com.xianyun.rpc;

import com.xianyun.rpc.loadBalance.LoadBalancer;
import com.xianyun.rpc.loadBalance.LoadBalancerFactory;
import com.xianyun.rpc.loadBalance.LoadBalancerKeys;
import com.xianyun.rpc.model.ServiceMetaInfo;
import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author xianYun
 * @version 1.0
 **/
public class LoadBalancerTest {
    final LoadBalancer loadBalancer = LoadBalancerFactory.getInstance(LoadBalancerKeys.CONSISTENT_HASH);

    /**
     * 测试 Spi 加载负载平衡器
     */
    @Test
    public void testLoadBalancer() {
        System.out.println(
                loadBalancer
        );
    }

    @Test
    public void select(){
        HashMap<String, Object> requestMap = new HashMap<>();
        ServiceMetaInfo serviceMetaInfo1 = new ServiceMetaInfo();
        serviceMetaInfo1.setServiceName("com.xianyun.service.HelloService");
        serviceMetaInfo1.setServiceHost("127.0.0.1");
        serviceMetaInfo1.setServicePort(8080);
        serviceMetaInfo1.setServiceGroup("default");
        ServiceMetaInfo serviceMetaInfo2 = new ServiceMetaInfo();
        serviceMetaInfo2.setServiceName("com.xianyun.service.HelloService");
        serviceMetaInfo2.setServiceHost("127.0.0.1");
        serviceMetaInfo2.setServicePort(8081);
        serviceMetaInfo2.setServiceGroup("default");
        List<ServiceMetaInfo> list = Arrays.asList(serviceMetaInfo1, serviceMetaInfo2);
        for (int i = 0; i < 5; i++) {
            requestMap.put("requestId", i + String.valueOf(i + 190));
            ServiceMetaInfo serviceMetaInfo = loadBalancer.select(requestMap, list);
            System.out.println(serviceMetaInfo);
            Assert.assertNotNull(serviceMetaInfo);
        }
    }
}
